﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PokerStar.Core;
using PokerStar.Core.Rules;

namespace PokerStar.Test
{
    [TestClass]
    public class DealerTest
    {
        [TestMethod]
        public void DeckOfCardsTest()
        {
            DeckOfCards deck = new DeckOfCards();

            TestContext.WriteLine("DeckOfCards init...");
            TestContext.WriteLine(deck.ToString());
            TestContext.WriteLine("");

            for (int i = 0; i < 5; i++)
            {
                TestContext.WriteLine("DeckOfCards {0}...", i + 1);
                deck.Reset();
                TestContext.WriteLine(deck.ToString());
                TestContext.WriteLine("");
            }
        }

        [TestMethod]
        public void GameTest()
        {
            Core.Game game = new Core.Game(9);
            game.AddPlayer(new Player("Miles"));
            game.AddPlayer(new Player("Yoyo"));
            game.AddPlayer(new Player("Vincent"));
            game.AddPlayer(new Player("Jun"));
            game.AddPlayer(new Player("Helen"));
            game.AddPlayer(new Player("阿兰哥"));
            game.AddPlayer(new Player("高歌"));
            game.AddPlayer(new Player("Wayne"));

            for (int i = 0; i < 3; i++)
            {
                game.Play();
                TestContext.WriteLine(game.GetStatistics());
            }

        }

        [TestMethod]
        public void GenerateCombinationsTest()
        {
            var list = Enumerable.Range(1, 5).ToList();
            var combination = new List<IList<int>>();
            //list.AddRange(new[] { 1, 2, 3 });

            MathHelper.GenerateCombinations(list, 5, 5, combination);

            foreach (var comb in combination)
            {
                foreach (var i in comb)
                {
                    TestContext.WriteLine(i.ToString());
                }
                TestContext.WriteLine("");
            }
        }


        [TestMethod]
        public void GameResultTest()
        {
            IList<Player> players = new List<Player>();

            // 1
            var player = new Player("Miles");
            player.Result = new FlushGameResult(Suit.Clubs);
            player.Result.Cards = new List<Card>
                {
                    new Card(Suit.Clubs, '2'), 
                    new Card(Suit.Clubs, '3'), 
                    new Card(Suit.Clubs, '5'), 
                    new Card(Suit.Clubs, '6'), 
                    new Card(Suit.Clubs, '7')
                };
            player.Result.Evidence = player.Result.Cards.Select(p => CardNumberComparer.Card2Int(p.Number)).ToList();
            players.Add(player);

            // 2
            player = new Player("Yoyo");
            player.Result = new StraightFlushGameResult(Suit.Clubs);
            player.Result.Cards = new List<Card>
                {
                    new Card(Suit.Clubs, 'A'),
                    new Card(Suit.Clubs, 'Q'), 
                    new Card(Suit.Clubs, 'J'), 
                    new Card(Suit.Clubs, 'T'), 
                    new Card(Suit.Clubs, 'K'), 
                };
            player.Result.Evidence = player.Result.Cards.Select(p => CardNumberComparer.Card2Int(p.Number)).ToList();
            players.Add(player);

            // 3
            player = new Player("V");
            player.Result = new FullhouseGameResult();
            player.Result.Cards = new List<Card>
                {
                    new Card(Suit.Clubs, '9'), 
                    new Card(Suit.Diamonds, '9'), 
                    new Card(Suit.Clubs, 'J'), 
                    new Card(Suit.Hearts, '9'), 
                    new Card(Suit.Spades, 'J')
                };
            player.Result.Evidence = new List<int> { 9, 11, 0, 0, 0 };
            players.Add(player);

            // 4
            player = new Player("W");
            player.Result = new StraightGameResult();
            player.Result.Cards = new List<Card>
                {
                    new Card(Suit.Clubs, 'T'),
                    new Card(Suit.Clubs, 'J'), 
                    new Card(Suit.Hearts, 'Q'), 
                    new Card(Suit.Clubs, 'K'), 
                    new Card(Suit.Clubs, 'A'), 
                };
            player.Result.Evidence = player.Result.Cards.Select(p => CardNumberComparer.Card2Int(p.Number)).ToList();
            players.Add(player);


            GameResultCollection rc = new GameResultCollection(players);

            TestContext.WriteLine("Winner: ");
            int index = 0;
            foreach (var rate in rc.GetRates())
            {
                index++;
                foreach (var p in rate)
                {
                    TestContext.WriteLine(" {0} {1} -> {2}\r\n", index, p, p.Result);
                }
            }
        }
        public TestContext TestContext { get; set; }
    }
}
